﻿using System;
using AccessControl.Wpf.ViewModels;
using AccessControl.Contracts.Managers;
using System.Windows.Input;
using AccessControl.Wpf.Commands;
using AccessControl.Contracts.Models;
using AccessControl.Wpf.Entries.ViewModels;

namespace AccessControl.Wpf.Maps.ViewModels
{
	class MapsViewModel : ViewModel, IActionViewModel
	{
		#region Fields

		IMapsManager _MapsManager;
		double _LeftOffset = 0, _TopOffset = 0, _VisibleWidth = 0, _VisibleHeight = 0;

		#endregion

		#region Public Constructors

		public MapsViewModel(IMapsManager mapsManager)
		{
			MapsManager = mapsManager;
		}

		#endregion

		#region Public Properties

		public IMapsManager MapsManager
		{
			get { return _MapsManager; }
			private set
			{
				if (_MapsManager != value)
				{
					_MapsManager = value;
					NotifyPropertyChanged("MapsManager");
				}
			}
		}

		public double LeftOffset
		{
			get { return _LeftOffset; }
			set
			{
				if (_LeftOffset != value)
				{
					_LeftOffset = value;
					NotifyPropertyChanged("LeftOffset");
				}
			}
		}

		public double TopOffset
		{
			get { return _TopOffset; }
			set
			{
				if (_TopOffset != value)
				{
					_TopOffset = value;
					NotifyPropertyChanged("TopOffset");
				}
			}
		}

		public double VisibleWidth
		{
			get { return _VisibleWidth; }
			set
			{
				if (_VisibleWidth != value)
				{
					_VisibleWidth = value;
					NotifyPropertyChanged("VisibleWidth");
				}
			}
		}

		public double VisibleHeight
		{
			get { return _VisibleHeight; }
			set
			{
				if (_VisibleHeight != value)
				{
					_VisibleHeight = value;
					NotifyPropertyChanged("VisibleHeight");
				}
			}
		}
		
		#endregion

		#region Commands

		public ICommand ShowEntryDetailsCommand
		{
			get { return new CommandBaseOfT<IMapEntry>(ShowEntryDetails); }
		}

		#endregion

		#region IActionViewModel Members

		public Contracts.Models.Action ViewModelAction
		{
			get { return Contracts.Models.Action.Operations; }
		}

		#endregion

		#region Private Members

		private void ShowEntryDetails(IMapEntry entry)
		{
			/* Create the entry view model */
			EntryViewModel viewModel = new EntryViewModel() { Entry = entry, MessagingService = MessagingService, PopupService = PopupService };
			
			/* Add the cameras */
			foreach (ICamera camera in entry.Cameras)
				viewModel.Cameras.Add(new CameraViewModel(camera));
			
			/* Start the cameras */
			foreach (CameraViewModel cameraViewModel in viewModel.Cameras)
				cameraViewModel.StartVideo();
			
			/* And show the popup */
			PopupService.ShowPopup(viewModel);

			/* Now close */
			viewModel.Close();
		}

		#endregion
	}
}
